package io.github.hylexus.jt808.samples.annotation.serivce.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.github.hylexus.jt808.samples.annotation.entity.req.LocationUploadRequestMsgBody;
import io.github.hylexus.jt808.samples.annotation.serivce.TerminalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;

/**
 * Created At 2020-06-18 18:09
 *
 * @author hylexus
 */
@Service
@Slf4j
public class TerminalServiceImpl implements TerminalService {
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void submitSPTLocation(String terminalId, LocationUploadRequestMsgBody msgBody) {
        new Thread(() -> {
            try {
                String akBody = getAKBody();
                JSONObject akJson = JSON.parseObject(akBody);  //响应json字符串转成json对象
                String ak = akJson.getString("ak");   //获取ak串
                if(ak == null || "".equals(ak)){
                    log.error("ak获取失败!");
                }
                //---start  开始封装上传数据
                Map<String,Object> map = new Hashtable<>();
                map.put("ak",ak);
                map.put("deviceId",terminalId);
                map.put("mhyPlate","2-GB002266");
                map.put("lng",msgBody.getLng());
                map.put("lat",msgBody.getLat());
                map.put("company","河南中诺检测技术服务有限公司");
                map.put("timestamp",System.currentTimeMillis());
                map.put("acc",msgBody.getStatusInfo().isAccStatus() ? 1 : 0);
                //---end  封装完毕
                System.out.println(map);
                String s = sendLocation(map);
                System.out.println(s);
            }catch (Exception e){
                log.error("上传位置信息失败!");
                e.printStackTrace();
            }
        }).start();
    }

    public String getAKBody() {
        String url = "https://www.hnsfyjxgk.com:7255/datasync/auth";
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("username", "ZNJC_015");
        jsonMap.put("password", "Sie8Lnf29");
        ResponseEntity<String> apiResponse = restTemplate.postForEntity
                (
                        url,
                        generatePostJson(jsonMap),
                        String.class
                );
        return apiResponse.getBody();
    }

    public HttpEntity<Map<String, Object>> generatePostJson(Map<String, Object> jsonMap) {
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        httpHeaders.setContentType(type);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(jsonMap, httpHeaders);
        return httpEntity;
    }

    public String sendLocation(Map<String,Object> map){
        String url = "http://www.hnsfyjxgk.com:9692/terminal/dynamic/json";
        ResponseEntity<String> apiResponse = restTemplate.postForEntity
                (
                        url,
                        generatePostJson(map),
                        String.class
                );
        return apiResponse.getBody();
    }

    public static void main(String[] args) {
        System.out.println(UUID.randomUUID());
    }
}
